package sv.gob.mtps.sib.controller;

import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import sv.gob.mtps.sib.model.Fuentefondo;
import sv.gob.mtps.sib.service.FondosService;

@Controller
public class FondosController {

	@Autowired
	private FondosService services;
	@RequestMapping(value = "/admin/showFondos")
	public  String showFondos(@ModelAttribute("fondos") Fuentefondo fuente,BindingResult result,Map<String, Object> map){
		map.put("fuentefondo", new Fuentefondo());
		map.put("fondosList", services.listFondo());
		return "admin/fuenteFondosAgregar";		
	}
	

	
	@RequestMapping(value="/admin/addFondos",method = RequestMethod.POST)
	public String addFondos(@ModelAttribute("fuenteFondos") Fuentefondo fuenteFondos, BindingResult result ){
		try{
			services.addFondo(fuenteFondos);
		}
		catch(DataAccessException ex){
			ex.printStackTrace();
		}
		
		return "redirect:/admin/showFondos";
	}
	
	
	@RequestMapping(value = "/admin/editFondos", method = RequestMethod.POST)
	public String editFondos(@ModelAttribute("fuenteFondos") Fuentefondo fuenteFondos,BindingResult result){			
			
			try{
				services.updateFondo(fuenteFondos);
			}
			catch(DataAccessException ex){
				ex.printStackTrace();
			}
			return "redirect:/admin/showFondos";
	}
	
	@RequestMapping(value = "/admin/delFondos", method = RequestMethod.POST)
	public String delFondos(@ModelAttribute("fuenteFondos") Fuentefondo fuenteFondos,BindingResult result){
		try{
			services.removeFondo(fuenteFondos.getIdFuentefondo());
		}
		catch(DataAccessException ex){
			ex.printStackTrace();
		}
		
		return "redirect:/admin/showFondos";
	}
	
	
	
}
